import { ref, unref, watch, onMounted } from 'vue'
import { post } from "/src/api/index";
import { ElMessage, ElMessageBox } from "element-plus";
import { getSelection, batchUpdateValue,fireExecute, getQuery } from '/src/utils/index'
export function init(self) {
  const module = self.module.value
  self.custom = {
    columns : [
      {
        prop: 'workspace_name',
        order: 'asc',
        width: 100,
      },
      {
        prop: 'project_name',
        order: 'asc',
        width: 110,
      },
      {
        prop: 'project',
        orde: 'asc',
        width: 100,
      },
      {
        prop: 'package_name',
        order: 'asc',
        width:300,
      },
      {
        prop: 'file_type',
        order: 'asc',
        width: 80,
      },
      {
        prop: 'name',
        order: 'asc',
        width: 350,
      },
      {
        prop: 'name_diff',
        label: '比对结果',
        width: 100,
      },
      {
        prop: 'file_template',
        width: 80,
      },
      {
        prop: 'no_overwrite',
        width: 90,
      },
      {
        prop: 'extra_info',
        width: 100,
      },
      
     ],

    editColumns : [
      {
        prop: 'workspace_name',
        label: '工作区',
        list_remote: {
          module: 'sdp_workspace', method:'list',
          query: {},
          sortInfo: ['name'],
          mapInfo: {label:'name', value:'name'}
        },
        type: 'select',
        required: true,
        onchange: param => {
          let {mode, form, column, columns, get_remote_list, set_query_storage} = param
          if (mode.indexOf('init') < 0) {
            // form.project_name = ''
          }
          get_remote_list({...param, columns,
            prop: 'project_name', query: {workspace_name: form[column.prop]}})
          .then(param => {
            const { data } = param
            if (data && data.length) {
              if (form.project_name && !data.find(ele => ele.name == form.project_name)) {
                form.project_name = ''
              }
            }
          })
        },
      },
      {
        prop: 'project_name',
        placeholder: '项目',
        type: 'select',
        list_remote: {
          module: 'sdp_project', method:'list',
          mode: 'dynamic',
          query: {},
          sortInfo: ['workspace_name', 'name'],
          mapInfo: {label:'name', value:'name'}
        },
      },
      {
        prop: 'name',
        required: true,
      },
      {

      },
      {
        prop: 'file_template',
        type: 'textarea',
        rows: 10,
      },
      {

      },
      {
        prop: 'no_overwrite',
        type: 'switch',
      },
      {
        prop: 'extra_info',
        type: 'textarea',
        rows: 5,
      },
    ],

    queryColumns : [
      {
        prop: 'workspace_name',
        fixed: true,
        list_remote: {
          module: 'sdp_workspace', method:'list',
          query: {},
          sortInfo: ['name'],
          mapInfo: {label:'name', value:'name'}
        },
        type: 'select',
      },
      {
        prop: 'name_like',
        label: '名称',
      },
      {
        prop: 'file_template_like',
        label: '模板',
      },
    ],

    buttons: [
      {
        prop: 'fireExecute',
        label: '生成文件',
        loading: false,
        type: 'success',
        onclick: (param) => {
          fireExecute({param, self})
        }
      },
      {
        prop: 'handleDiff',
        label: '比对',
        loading: false,
        onclick: (param) => {
          let { button } = param
          let { query, selectRows } = self
          query = unref(query)
          selectRows = unref(selectRows)
          if (!selectRows || !selectRows.length || query.workspace_name) {
            selectRows = unref(self.tableData)
          }
          if (!selectRows || !selectRows.length) {
            ElMessage.error(`必须选择至少一个条目进行比对`);
            return;
          }

          let diffRows = selectRows.filter(ele => ele.workspace_name != selectRows[0].workspace_name)

          if (diffRows && diffRows.length) {
            ElMessage.error(`仅可选择同一工作区条目进行比对。`);
            return;
          }
          ElMessageBox.prompt('请输入比对工作区名称', '比对模板', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            closeOnPressEscape:false,
            closeOnClickModal:false,
            inputValidator: (val) => {
              if (val === null||val.length < 1) {
                return false;
              }
            },
            inputErrorMessage: '此项不能为空',
          })
            .then(param  => {
              let { value : workspace_name } = param
              if (!workspace_name) {
                ElMessage.error(`必须输入工作区`);
                return;
              }
              if (workspace_name == selectRows[0].workspace_name) {
                ElMessage.error(`不可与当前工作区进行比对`);
                return;
              }
              button.loading = true;
              post('sdp_workspace', 'list', {name:workspace_name})
              .then((data) => {
                if (!data || !data.length) {
                  ElMessage.error(`工作区不存在：${workspace_name}`);
                  button.loading = false;
                  return;
                }
                self.handleSearch({
                  pageNo: 1,
                  pageSize: 10000,
                  queryExtraParam: {
                    workspace_name,
                    name: null,
                    file_template: null,
                    name_like: null,
                    file_template_like: null
                  },
                  withoutSetData: true,
                  withoutProcessParam:  true,
                }).then(data => {
                  button.loading = false
                  for(let item in selectRows) {
                    item = selectRows[item]
                    let foundItem, foundItemByName
                    for(let oriItem in data) {
                      oriItem = data[oriItem]
                      if (oriItem.name == item.name && oriItem.file_type == item.file_type) {
                        foundItemByName = oriItem
                      }
                    }
                    for(let oriItem in data) {
                      oriItem = data[oriItem]
                      if (oriItem.name == item.name && !foundItemByName) {
                        foundItemByName = oriItem
                      }
                      if (oriItem.file_type == item.file_type
                        && oriItem.name == item.name
                        && oriItem.project == item.project
                        && oriItem.project_name == item.project_name
                        && oriItem.package_name == item.package_name
                      ) {
                        foundItem = oriItem;
                        break;
                      }
                    }
                    if (foundItem) {
                      if (foundItem.file_template != item.file_template) {
                        item.name_diff= '不一致'
                        item.file_template_new = foundItem.file_template
                        item.workspace_name_new = foundItem.workspace_name
                        item.id_new = foundItem.id
                      } else {
                        item.name_diff= '相同'
                      }
                    } else if (foundItemByName) {
                      if (foundItemByName.file_template != item.file_template) {
                        item.name_diff = '不一致-'+foundItemByName.project_name
                      } else {
                        item.name_diff = '-'+foundItemByName.project_name
                      }
                      item.file_template_new = foundItemByName.file_template
                      item.workspace_name_new = foundItemByName.workspace_name
                      item.id_new = foundItemByName.id
                    } else {
                      item.name_diff = '未找到'
                    }
                  }
                })
              })
            })
        }
      },
      {
        prop: 'editTemplate',
        label: '编辑模板',
        loading: false,
        onclick: (param) => {
          let dialogInfo = unref(self.editTemplateDialogInfo)
          let {query, selectRows} = param
          if (!selectRows || selectRows.length == 0) {
            ElMessage.error(`请选择一条记录后再操作`);
            return;
          }
          let item = selectRows[0]

          dialogInfo.title = '编辑模板。工作区：'+item.workspace_name+'，工程：'+item.project_name+'，名称：'+item.name
          dialogInfo.is_diff = false
          dialogInfo.show({...param, dialogInfo, query: unref(param.query)})
        }
      },
    ],

    remoteConfig : {
    },

    onInit: () => {
      self.showDiff = (param) => {
        let dialogInfo = unref(self.editTemplateDialogInfo)
        dialogInfo.title = '比对模板。左-当前工作区：'+param.row.workspace_name+'，右-其他工作区：'+param.row.workspace_name_new+'，名称：'+param.row.name
        dialogInfo.is_diff = true;

        //amis污染了window.MonacoEnvironment，需要删除，否则比对结果的背景颜色不正常
        delete window.MonacoEnvironment

        dialogInfo.show({...self, dialogInfo, query: unref(self.query), selectRows:[param.row]})

      }
      let dialogInfo = unref(self.editDialogInfo)
      dialogInfo.onconfirm_ori = dialogInfo.onconfirm
      dialogInfo.onconfirm = (param) => {
        let tableData = [...unref(self.tableData)]
        return dialogInfo.onconfirm_ori(param)
        .then( data => {
          unref(self.tableData).forEach( ele => {
            let item = tableData.find(e => e.id == ele.id)
            if (item && item.name_diff) {
              ele.name_diff = item.name_diff
            }
          })
        })
      }
    }

  }
}

